<template>
  <div>
    <div class="common_form_search">
      <el-form ref="searchForm" :inline="true" :model="searchForm">
        <el-form-item label="手机号">
          <el-input v-model="searchForm.mobile" maxlength="11" placeholder="请输入手机号" clearable @input="val => changeNumberInput(val)" />
        </el-form-item>
        <el-form-item label="发送场景">
          <el-select v-model="searchForm.template_id" style="width:230px" placeholder="请选择发送场景" clearable>
            <el-option v-for="item in templateList" :key="item.id" :label="item.sms_title" :value="item.id" />
          </el-select>
        </el-form-item>
        <el-form-item label="短信状态">
          <el-select v-model="searchForm.send_status" placeholder="全部" clearable>
            <el-option label="发送成功" :value="1" />
            <el-option label="发送失败" :value="2" />
          </el-select>
        </el-form-item>
        <el-form-item label="供应商名称">
          <el-input v-model="searchForm.shopname" placeholder="请输入供应商名称" clearable />
        </el-form-item>
        <el-form-item label="发送时间">
          <my-date-picker ref="myDatePicker" />
        </el-form-item>
        <el-form-item>
          <el-button size="small" type="primary" @click="search()">查询</el-button>
          <el-button size="small" @click="reset()">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <el-divider />
    <div class="flex align-center table-top-btns">
      <h5 class="flex align-center">
        <svg-icon style="margin-right:15px" icon-class="list" :size="15" />数据列表
      </h5>
    </div>
    <el-table :data="tableData" border>
      <el-table-column align="center" prop="id" width="60" label="ID" />
      <el-table-column align="center" width="190" label="关联订单号">
        <template slot-scope="scope">
          <span class="blue-font pointer" @click="goToOrder(scope.row.order_id)">{{ scope.row.order_id }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" prop="mobile" width="120" label="手机号" />
      <el-table-column align="center" prop="sms_title" label="发送场景" />
      <el-table-column align="center" prop="content" label="短信内容" min-width="400" />
      <el-table-column align="center" prop="charging_num" label="短信数量" />
      <el-table-column align="center" label="短信状态" width="90">
        <template slot-scope="scope">
          <span class="status_icon" :class="{ 'success': scope.row.send_status == 1 }">{{ scope.row.send_status_name }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" prop="reason" label="原因" width="100" />
      <el-table-column align="center" prop="create_time" width="100" label="发送时间" />
      <el-table-column align="center" prop="shopname" width="150" label="供应商名称" />
    </el-table>
    <custom-page ref="customPage" :total="total" @getList="getList" />
  </div>
</template>

<script>
import myDatePicker from '@/components/myDatePicker'
import customPage from '@/components/customPage'
import { getSmsTemplate, msmUseList } from '@/api/data'
export default {
  components: { myDatePicker, customPage },
  data() {
    return {
      searchForm: {
        mobile: '',
        template_id: '',
        send_status: '',
        create_time: '',
        shopname: ''
      },
      templateList: [],
      tableData: [],
      total: 0
    }
  },
  mounted() {
    this.getSelect()
    this.getList()
  },
  methods: {
    goToOrder(orderId) {
      if (orderId) {
        this.$router.push({ name: 'orderList', params: { orderId }})
      }
    },
    // 查询
    search() {
      const pickerVal = this.$refs.myDatePicker.getVal()
      if (pickerVal !== 'error') {
        this.searchForm.create_time = pickerVal
        this.$refs.customPage.page = 1
        this.getList()
      }
    },
    // 重置
    reset() {
      if (this.$refs.myDatePicker) {
        this.$refs.myDatePicker.reset()
      }
      this.searchForm = {
        mobile: '',
        template_id: '',
        send_status: '',
        create_time: '',
        shopname: ''
      }
      this.getList()
    },
    changeNumberInput(value) {
      const pattern = /^[1-9][0-9]*$/
      if (!pattern.test(value)) {
        // input 框绑定的内容为空
        this.searchForm.mobile = ''
      }
    },
    getList() {
      const { page, pageSize } = this.$refs.customPage
      msmUseList({
        page,
        pageSize,
        ...this.searchForm
      }).then(res => {
        const { data, total } = res
        this.tableData = data || []
        this.total = total || 0
      })
    },
    getSelect() {
      getSmsTemplate().then(res => {
        this.templateList = res || []
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.status_icon::before {
  display: inline-block;
  content: "";
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background-color: #ff4146;
  margin-right: 6px;
}

.status_icon.success::before {
  background-color: #4ccb86;
}
</style>
